<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组并集，交集，差集</title>
</head>

<body>
    <script>
        var arr1 = [1, 2, 3, 4, 5];
        var arr2 = [4, 5, 6, 7, 8];

        var a = new Set(arr1);
        var b = new Set(arr2);

        //数组并集：a + b，然后去掉重复的元素
        var c = new Set([...a,...b]);
        //数组交集：a和b共同拥有的部分
        var d = new Set([...a].filter(x =>b.has(x)));
        //差集：a - b，即在 a中，去掉和b共同拥有的部分
        var e = new Set([...a].filter(x => !b.has(x)));

        console.log(c);
        console.log(d);
        console.log(e);
    </script>
</body>

</html>